const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active:0,
    page: 1,
    finished: false,
    americanFans:[],
    statistics:{},
    rankingList:[
      'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/gold_medal.png',
      'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/silver_medal.png',
      'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/bronze_medal.png'
    ],
    current_member:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
    this.getStatistics();
  },
  getData () {
    if (this.data.finished) return;
    let url = this.data.active  == 1 ? "plugin.powder-value.frontend.stat.detailed" : "plugin.powder-value.frontend.stat.ranks"
    let baseUrl = app.getNetAddresss(url);
    app._getNetWork({
      url: baseUrl,
      data: {
        page: this.data.page
      },
      success: (respone) => {
        if (respone.data.result !== 1) {
          wx.showToast({
            title: respone.data.msg,
          });
          return false;
        }
        let res = respone.data.data;
        let finished = false;
        let americanFans = [];
        if(this.data.active == 0) { 
          if (res.ranks.data.length === 0 || res.ranks.data.length < res.ranks.per_page || res.ranks.current_page === res.ranks.last_page) {
            finished = true;
          }
          americanFans = this.data.americanFans.concat(res.ranks.data);
          if(res.current_member.constructor === Object && this.data.page == 1) {
            americanFans.unshift(res.current_member);
            this.setData({current_member:true})
          }
          for(let item of americanFans) {
            item.member.username = item.member.username.slice(0,2) + '****';
          }
        }else {              
          if (res.data.length === 0 || res.data.length < res.per_page || res.current_page === res.last_page) {
            finished = true;
          }
          americanFans = this.data.americanFans.concat(res.data);
        }

        this.setData({
          finished,
          americanFans,
          page: ++this.data.page
        });
      }
    });
  },
  getStatistics() {
    let urlStr = app.getNetAddresss("plugin.powder-value.frontend.stat.index");
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result != 1) return;
        this.setData({
          statistics: res.data,
        });
        wx.setNavigationBarTitle({
          title: this.data.statistics.diy_name,
        });
      },
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getData();
  },
  changeTabs(evt) {
    this.setData({
      page: 1,
      finished: false,
      active:evt.detail.name,
      americanFans:[]
    })
    this.getData();
  }
})